﻿#define _DebugDrawBorder_uomBHP_ExpandClientRectange

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DienLib.SummitLib;
//using Excel = Microsoft.Office.Interop.Excel;
//using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;

namespace DienLib.SummitLib
{
    /// <summary>
    /// Wrapper for Summit New Job Form
    /// </summary>
    public class ExpSafeLib : IDisposable
    {
        #region Remote VARIABLE
        const string EXPSAFETY_BUT_CANCEL = "_btnCancel";
        ButtonD a_btnCancel;
        const string EXPSAFETY_BUT_OK = "_btnOK";
        ButtonD a_btnOK;
        const string EXPSAFETY_RBUT_GROUNDCLAMP_YES = "_RadGroundYes";
        RadioButtonD a_RadGroundYes;
        const string EXPSAFETY_RBUT_KEYREMOVE_YES = "_radKeyRemYes";
        RadioButtonD a_radKeyRemYes;
        const string EXPSAFETY_RBUT_RADIOSILENT_YES = "_RadRadioYes";
        RadioButtonD a_RadRadioYes;
        const string EXPSAFETY_RBUT_REDDET_YES = "_RadRedYes";
        RadioButtonD a_RadRedYes;
        const string EXPSAFETY_RBUT_KEYOUTSIDE_YES = "_RadSafetyKeyYes";
        RadioButtonD a_RadSafetyKeyYes;
        const string EXPSAFETY_RBUT_STRAYTEST_YES = "_RadStrayYes";
        RadioButtonD a_RadStrayYes;
        #endregion

        private Form _form;
        public ExpSafeLib(Form ExpSafetyForm)
        {
            if (ExpSafetyForm == null) throw new NullReferenceException();
            if (ExpSafetyForm.IsDisposed) throw new InvalidOperationException("Explosive Safety Form already Disposed or Closed !");
            this._form = ExpSafetyForm;
            #region Init Remote Variable
            a_btnCancel = new ButtonD(DienLib.Reflector.GetField<Button>(_form, EXPSAFETY_BUT_CANCEL));
            a_btnOK = new ButtonD(DienLib.Reflector.GetField<Button>(_form, EXPSAFETY_BUT_OK));
            a_RadGroundYes = new RadioButtonD(DienLib.Reflector.GetField<RadioButton>(_form, EXPSAFETY_RBUT_GROUNDCLAMP_YES));
            a_radKeyRemYes = new RadioButtonD(DienLib.Reflector.GetField<RadioButton>(_form, EXPSAFETY_RBUT_KEYREMOVE_YES));
            a_RadRadioYes = new RadioButtonD(DienLib.Reflector.GetField<RadioButton>(_form, EXPSAFETY_RBUT_RADIOSILENT_YES));
            a_RadRedYes = new RadioButtonD(DienLib.Reflector.GetField<RadioButton>(_form, EXPSAFETY_RBUT_REDDET_YES));
            a_RadSafetyKeyYes = new RadioButtonD(DienLib.Reflector.GetField<RadioButton>(_form, EXPSAFETY_RBUT_KEYOUTSIDE_YES));
            a_RadStrayYes = new RadioButtonD(DienLib.Reflector.GetField<RadioButton>(_form, EXPSAFETY_RBUT_STRAYTEST_YES));
            #endregion

            if (_form.InvokeRequired) _form.Invoke((MethodInvoker)delegate() { _form.TopMost = true; });
            else _form.TopMost = true;
        }

        public static ExpSafeLib FindExpSafeLib()
        {
            // Get Explosive Safety Checklist
            var list = DienLib.WinAPI.FindWindowLike(Process.GetCurrentProcess(), "Explosive Safety Summary", DienLib.FindCulture.EXACT_MATCH);
            if (list.Count == 0) return null;

            IntPtr hWnd = list[0]; // Only 1 Perf Window at a time

            if (hWnd == IntPtr.Zero) { MessageBox.Show("There is no Explosive Safety Window Open ! Please Click \"Explosive Safety Summary\" at Page 3 first !"); return null; }

            Form _formExpSafety;
            try { _formExpSafety = (Form)Control.FromHandle(hWnd); }
            catch (Exception ex) { MessageBox.Show("Cannot get Form Explosive Safety Summary from Handle ! Something wrong"); throw ex; }
            if (_formExpSafety == null) { MessageBox.Show("Cannot get Form Explosive Safety Summary from Handle ! Something wrong"); return null; }

            try { return new ExpSafeLib(_formExpSafety); }
            catch (Exception ex) { MessageBox.Show("Something Wrong when Create new ExpSafeLib object form Active Form !", "WARNING : Something wrong !\r\n" + ex.Message); return null; }
        }

        public void PerformClickOK()
        {
            a_btnOK.PerformClick(); // Try catch truong hop dong form bao NULL Button
        }

        public void PerformClickCANCEL()
        {
            a_btnCancel.PerformClick(); // Try catch truong hop dong form bao NULL Button
        }

        public void PerformFillFullYes()
        {
            a_radKeyRemYes.Checked = true;
            a_RadRedYes.Checked = true;
            a_RadStrayYes.Checked = true;
            a_RadRadioYes.Checked = true;
            a_RadGroundYes.Checked = true;
            a_RadSafetyKeyYes.Checked = true;
        }

        #region DISPOSE METHOD
        // Dispose() calls Dispose(true)
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // NOTE: Leave out the finalizer altogether if this class doesn't 
        // own unmanaged resources itself, but leave the other methods
        // exactly as they are. 
        ~ExpSafeLib()
        {
            // Finalizer calls Dispose(false)
            Dispose(false);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources, (detach event ?)
                if (this._form != null) this._form = null; // Khong keu this._refobj.Dispose vi Reference toi object do. Ngoai ra do pass reference nen this._refobj = null khong anh huong gi toi object goc
                if (this.a_btnCancel != null) this.a_btnCancel = null;
                if (this.a_btnOK != null) this.a_btnOK = null;
                if (this.a_RadGroundYes != null) this.a_RadGroundYes = null;
                if (this.a_radKeyRemYes != null) this.a_radKeyRemYes = null;
                if (this.a_RadRadioYes != null) this.a_RadRadioYes = null;
                if (this.a_RadRedYes != null) this.a_RadRedYes = null;
                if (this.a_RadSafetyKeyYes != null) this.a_RadSafetyKeyYes = null;
            }

            /* free native resources if there are any.
            if (nativeResource != IntPtr.Zero) 
            {
                Marshal.FreeHGlobal(nativeResource);
                nativeResource = IntPtr.Zero;
            }*/
        }
        #endregion

    }
}


